﻿//-----------------------------------------------------------------------
// <copyright file="MaterialTypeMetaConverter.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Converters
{
    using System;
    using System.Reflection;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Models;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Models.Attributes;
    using Windows.UI.Xaml.Data;

    /// <summary>
    /// Class MaterialTypeMetaConverter
    /// </summary>
    public class MaterialTypeMetaConverter : IValueConverter
    {
        /// <summary>
        /// Gets or sets a value indicating whether [return display name].
        /// </summary>
        /// <value><c>true</c> if [return display name]; otherwise, <c>false</c>.</value>
        public bool ReturnDisplayName { get; set; }

        /// <summary>
        /// Converts the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="language">The language.</param>
        /// <returns>System.Object.</returns>
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var displayName = string.Empty;
            var imageUrl = string.Empty;
            var type = typeof(MaterialType);
            var memInfo = type.GetRuntimeField(value.ToString());
            var attributes = memInfo.GetCustomAttributes(false);
            foreach (var attribute in attributes)
            {
                if (attribute is DisplayNameAttribute)
                {
                    displayName = (attribute as DisplayNameAttribute).Name;
                }

                if (attribute is ImageAttribute)
                {
                    imageUrl = (attribute as ImageAttribute).ImagePath;
                }
            }

            if (this.ReturnDisplayName)
            {
                return displayName;
            }
            
            return imageUrl;
        }

        /// <summary>
        /// Converts the back.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="language">The language.</param>
        /// <returns>System.Object.</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object ConvertBack(object value, System.Type targetType, object parameter, string language)
        {
            throw new System.NotImplementedException();
        }
    }
}
